﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RealmMudLib.Encryption;
using Debugger;
using System.IO;

namespace RealmMud.Protocols
{
    public class GameServer
    {
        public GameClient GameObject { get; private set; }
        public byte[] Message { get; private set; }
        private MemoryStream ByteStream { get; set; }

        public GameServer(GameClient client, byte[] message)
        {
            this.GameObject = client;
            this.Message = message;
            // Handle the message
            this.HandlePacket();
        }
        private void HandlePacket()
        {
            byte[] msg = null;
            if (this.GameObject.Key == null || this.GameObject.IV == null)
            {
                string pass = "#UgMugaBugStug483#";
                // Decrypt it
                mainForm.WriteLine(string.Format("Length of packet {0}", Message.Length));
                msg = EncDec.Decrypt(this.Message, pass);
            }
            else { msg = EncDec.Decrypt(this.Message, this.GameObject.Key, this.GameObject.IV); }
            // Begin processing the unencrypted message
            ByteStream = new MemoryStream(msg);
            using (BinaryReader reader = new BinaryReader(ByteStream))
            {
                // Get the packet ID
                int ID = reader.ReadInt32();
                mainForm.WriteLine(string.Format("Packet Id {0}", ID));
                byte[] remainder = reader.ReadBytes((int)reader.BaseStream.Length);
                this.ProcessPacket(ID, remainder);
            }
        }
        private void ProcessPacket(int id, byte[] packet)
        {
            switch (id)
            {
                case 10:
                    {
                        // Encryption packet
                        string data = Encoding.ASCII.GetString(packet);
                        string[] dataSplit = data.Split(':');
                        // First is our key
                        byte[] Key = Encoding.ASCII.GetBytes(dataSplit[0]);
                        // Then our IV
                        byte[] IV = Encoding.ASCII.GetBytes(dataSplit[1]);
                        this.GameObject.Key = Key;
                        this.GameObject.IV = IV;
                        mainForm.WriteLine(string.Format("Our key is now {0} and our IVKey is {1}",
                            Encoding.ASCII.GetString(Key),
                            Encoding.ASCII.GetString(IV)));
                        // Send back to server, letting them know we got our keys
                        byte[] buffer = new byte[] { 0xAA, 0xBB};
                        // this is packet ID of 11
                        
                        string reply = "KEYSRECEIVED";
                        List<byte> pack = new List<byte>();
                        byte[] pckId = BitConverter.GetBytes(11);
                        byte[] msg = Encoding.ASCII.GetBytes(reply);
                        pack.AddRange(pckId);
                        pack.AddRange(msg);

                        byte[] encoded = EncDec.Encrypt(pack.ToArray(), this.GameObject.Key, this.GameObject.IV);
                        this.GameObject.Client.Client.Send(encoded);
                    }
                    break;
                case 12:
                    {
                        mainForm.WriteLine("GOT OK FROM SERVER ENCRYPTION WORKS!!");
                    }
                    break;
            }
            mainForm.WriteLine(string.Format("Remaining Bytes {0}", Encoding.ASCII.GetString(packet)));
        }
    }
}
